

#import "CYLDBManager.h"

NSString *const kDataSourceSectionKey     = @"Symptoms";
NSString *const kDataSourceCellTextKey    = @"grade";
NSString *const kDataSourceCellPictureKey = @"Picture";

@implementation CYLDBManager

/**
 *  懒加载_dataSource
 *
 *  @return NSMutableArray
 */
+ (NSMutableArray *)dataSource
{
        static NSMutableArray *dataSource = nil;
        static dispatch_once_t dataSourceOnceToken;
        dispatch_once(&dataSourceOnceToken,^{
            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data.json" ofType:nil];
            NSData *data = [[NSFileManager defaultManager] contentsAtPath:filePath];
            NSError *error;
            if (data) {
                dataSource = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            }
        });

    return dataSource;
    
}

/**
 *  懒加载_allTags
 *
 *  @return NSMutableArray
 */
+ (NSMutableArray *)allTags
{
    static NSMutableArray *allTags = nil;
    static dispatch_once_t allTagsOnceToken;
    dispatch_once(&allTagsOnceToken,^{
        allTags = [NSMutableArray array];
        [[[self class] dataSource] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            @autoreleasepool {
                NSArray *symptoms = [NSArray arrayWithArray:[obj objectForKey:kDataSourceSectionKey]];
                [symptoms enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    [allTags addObject:[obj objectForKey:kDataSourceCellTextKey]];
                }];
            }
        }];
    });

    return allTags;
}

@end
